package cn.gson.boot.controller;

import cn.gson.boot.model.pojos.*;
import cn.gson.boot.model.service.FaHuoService;
import cn.gson.boot.model.service.HeTongService;
import cn.gson.boot.model.service.JiaoFuJiHuaService;
import cn.gson.boot.model.service.TuiHuoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/fahuo")
public class FaHuoController {
    @Autowired
    HeTongService heTongService;
    @Autowired
    FaHuoService faHuoService;
    @Autowired
    TuiHuoService tuiHuoService;

    @Autowired
    JiaoFuJiHuaService jiaoFuJiHuaService;

    //查看所有订单
    @RequestMapping("/fhckDingDan")
    @ResponseBody
    public List<Dingdan> bB(){
        List<Dingdan> dd = heTongService.fahuoSelectAllDingDan();
        List<Dingdan> dd2 = new ArrayList<>();
        List<Jiaofujihua> jj = null;
        for(int i=0;i<dd.size();i++){
            jj = fhckddJiaoJi(dd.get(i).getDdId());
            if(jj.size()==0){
                dd2.add(dd.get(i));
            }
        }
        dd.removeAll(dd2);
        return dd;
    }

    //<!--发货：查看所有订单 换货-->
    @RequestMapping("/fahuoSelectAllDingDan2")
    @ResponseBody
    public List<Dingdan> fahuoSelectAllDingDan2(){
        //return heTongService.fahuoSelectAllDingDan2();
        List<Dingdan> dd = heTongService.fahuoSelectAllDingDan2();
        List<Dingdan> ddok = new ArrayList<>();
        List<Fahuo> fh = faHuoService.selectAllFaHuo(null,null,null,null,null);
        for(int i=0;i<fh.size();i++){
            for (int j=0;j<dd.size();j++){
                //如果订单已经存在发货单中了，并且此发货单的审核状态不为否决
                if(fh.get(i).getDingdanByDdId().getDdId() == dd.get(j).getDdId() && !fh.get(i).getFhZhuangtai().equals("否决")){
                    ddok.add(dd.get(j));
                    break;
                }
            }
        }
        for(int k=0;k<dd.size();k++){
            for(int g=0;g<ddok.size();g++){
                if(dd.get(k).getDdId() == ddok.get(g).getDdId()){
                    dd.remove(k);
                    break;
                }
            }
        }
        return dd;
    }



    //根据订单id查看订单详情
    @RequestMapping("/fhselectDingDanByDdId")
    @ResponseBody
    public List<Dingdanxiangqing> fhselectDingDanByDdId(Integer ddid){
        return heTongService.selectDingDanByDdId(ddid);
    }
    //根据换货id查看换货详情
    @RequestMapping("/fhselectHuanHuoDanByDdId")
    @ResponseBody
    public List<Huanhuoxiangqing> fhselectHuanHuoDanByDdId(Integer hhId){
        return tuiHuoService.ckHuanHuoXiangQin(hhId);
    }

    //新增发货(审核)
    @RequestMapping("/addFaHuo")
    @ResponseBody
    public void addFaHuo(@RequestBody Fahuo fahuo){

        faHuoService.addFaHuo(fahuo);
    }
    //审核同意后，确定发货
    @RequestMapping("/quedingFaHuo")
    @ResponseBody
    public void quedingFaHuo(@RequestBody Fahuo fahuo){

        faHuoService.quedingFaHuo(fahuo);
    }

    //查看所有发货
    @RequestMapping("/selectAllFaHuo")
    @ResponseBody
    public Map<String,Object> selectAllFaHuo(String ddHao, String khName, String time, String fhZt,
                                             Integer pageNumm, Integer pageSizee, HttpSession session){
        Page<Object> objects = PageHelper.startPage(pageNumm, pageSizee);

        String time3 = null;
        if(time != null && time != "" && !time.equals("")){
            StringBuffer time1 = new StringBuffer(time.substring(0,10));
            System.err.println("tim1:"+time1);
            int ti = Integer.parseInt(time1.substring(8))+1;
            System.err.println("ti:"+ti);
            String tistr = toString().valueOf(ti);
            if(toString().valueOf(ti).length() == 1){
                tistr = 0 + toString().valueOf(ti);
            }
            StringBuffer time2 = new StringBuffer(time1.replace(8,time1.length(), tistr));
            time3 = time2.toString();
        }

        User us = (User)session.getAttribute("user");
        List<UserLogin> users = (List<UserLogin>)session.getAttribute("users");
        List<Role> roles = users.get(0).getRoleListss();
        List<String> jueNameList = new ArrayList<>();
        for(int i=0;i<roles.size();i++){
            jueNameList.add(roles.get(i).getJueName());
        }
        String empName = null;
        if(jueNameList.indexOf("boss")>-1 ||jueNameList.indexOf("销售经理")>-1  ){
            empName = null;
        }else{
            empName = us.getEmpName();
        }

        if(fhZt.equals("全部数据")){
            fhZt = null;
        }

        List<Fahuo> list = faHuoService.selectAllFaHuo(ddHao,khName,time3,fhZt,empName);
       /* System.out.println("订单号:::"+ddHao);
        System.out.println("objects.getTotal()2:"+objects.getTotal());
        System.out.println("pageNumm2:"+pageNumm);
        System.out.println("pageSize2e:"+pageSizee);*/

        Map<String,Object> map = new HashMap();
        map.put("total", objects.getTotal());
        map.put("list", list);
        return map;
    }

    //根据发货id查看发货详情
    @RequestMapping("/selectFaHuoXiangQin")
    @ResponseBody
    public List<Fahuoxiangqing> selectFaHuoXiangQin(Integer fhId){
        return faHuoService.selectFaHuoXiangQin(fhId);
    }

    //移除发货
    @RequestMapping("/yichuFaHuo")
    @ResponseBody
    public void yichuFaHuo(Integer fhId){
        faHuoService.yichuFaHuo(fhId);
    }

    //根据发货id查看发货
    @RequestMapping("/selectFaHuoByfhId")
    @ResponseBody
    public Fahuo selectFaHuoByfhId(Integer fhId){
        return faHuoService.selectFaHuoByfhId(fhId);
    }


    //根据交计id查看交计详情
    @RequestMapping("/fhselectjjXqbyjjId")
    @ResponseBody
    public List<Jiaofujihuaxiangqing> fhselectjjXqbyjjId(Integer jjId){
        return jiaoFuJiHuaService.ckJJXQbyjjId(jjId);
    }


    //根据选择的订单查看此订单交付计划状态为已出库的交付计划
    @RequestMapping("/fhckddJiaoJi")
    @ResponseBody
    public List<Jiaofujihua> fhckddJiaoJi(Integer ddId){
        List<Jiaofujihua> jj = jiaoFuJiHuaService.fhckddJiaoJi(ddId);//交付计划状态为已出库的交付计划
        List<Fahuo> ff = faHuoService.selectAllFaHuo
                (null, null, null, null, null);//所有的发货

        //记录要移除交付计划的下标
        List<Jiaofujihua> jj2 = new ArrayList<>();
      //判断交付计划是否存在与发货中
        for(int j=0;j<jj.size();j++){
            for(int f=0;f<ff.size();f++){
                if(ff.get(f).getJiaofujihua() != null){
                    //存在就移除
                    if(jj.get(j).getJjId() == ff.get(f).getJiaofujihua().getJjId()){
                        jj2.add(jj.get(j));
                        break;
                    }
                }
            }
        }
        System.out.println("jj2:"+jj2.size());
        System.out.println("jj:"+jj.size());
        if(jj2.size() == 0){
            return jj;
        }else {
            return jj2;
        }
    }



    /*//{退货/换货}据ddId查到此订单的所有发货状态为‘发货成功’的发货单
    @RequestMapping("/selectFaHuoByddIdTHfhcg")
    @ResponseBody
    public List<Fahuo> selectFaHuoByddIdTHfhcg(Integer ddId){
        return faHuoService.selectFaHuoByddIdTHfhcg(ddId);
    }
*/
   /* //退货 ==> 根据发货id查看发货详情
    @RequestMapping("/selectFaHuoXiangQinTuiHuo")
    @ResponseBody
    public List<Fahuoxiangqing> selectFaHuoXiangQinTuiHuo(Integer fhId){
        return faHuoService.selectFaHuoXiangQin(fhId);
    }

    //换货 ==> 根据发货id查看发货详情
    @RequestMapping("/selectFaHuoXiangQinHuanHuo")
    @ResponseBody
    public List<Fahuoxiangqing> selectFaHuoXiangQinHuanHuo(Integer fhId){
        return faHuoService.selectFaHuoXiangQin(fhId);
    }*/


}
